/*
 * Property Keys for the Transformer class. 
 *
 */
package com.google.cse.emea.universaltransformer;

import com.google.cse.emea.jct.utils.Profile;
import com.google.cse.emea.jct.utils.ProfileKeysInterface;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Property Keys for the Transformer class. Unfortunatly I had
 * to path all profile methods through this class. Otherwise
 * I would have to use the enumerations in a different way in
 * the transformer class.
 * @author jtrapp
 */
public enum TaskList implements ProfileKeysInterface {

    NAME("name"),
    DESCRIPTION("description"),
    VERSION("version"),
    AUTHOR("author"),
    DEFAULT_TASK("default"),
    TASKS("task"),
    LAST_TASK("task.active"),
    EDITORS("editor"),
    OUTPUT_FILE_FOLLOW_INPUT("output.file.followInput");


    /** Profile is used to get and store settings.*/
    private String key;
    static Class scope = null;

    private TaskList(String key) {
        this.key = key;
    }

    public String getName() {
        return key;
    }
    
    /** Store the last used profile in th preferences.
     * 
     * @param taskListConfiguration
     * @param taskConfiguration
     * @throws java.io.IOException
     */
    static void setProfile(Profile taskListConfiguration,
            Profile taskConfiguration) throws IOException {
        Vector<String> taskURLs = taskListConfiguration.loadList(TASKS);
        for (int i=0; i<taskURLs.size(); i++) {
            String taskURL = taskURLs.get(i);
            Profile p = Profile.get(scope, taskURL);
            if (p==taskConfiguration) {
            taskListConfiguration.put(LAST_TASK, i+"");
            }
        }
    }

    public static Vector<Profile> getTasks(Class scope, Profile profile) throws IOException {
        Vector<String> taskURLs = profile.loadList(TASKS);
        Vector<Profile> result = new Vector<Profile>();
        for (String taskURL : taskURLs) {
            result.add(Profile.get(scope, taskURL));
        }
        return result;
    }

    public static int getDefaultTaskIndex(Profile profile) {

        String lastTask = profile.get(LAST_TASK);
        if ((lastTask != null) && (lastTask.length() > 0)) {
            int index = profile.getInteger(LAST_TASK);
            return index;
        } else {
            String defaultTask = profile.get(DEFAULT_TASK);
            if ((defaultTask != null) && (defaultTask.length() > 0)) {
                int index = Integer.parseInt(defaultTask);

                return index;
            }
        }
        Vector<String> profiles = profile.loadList(TASKS);
        return profiles.size()>0? 0 : -1 ;
    }

    public static String getDefaultTaskName(Profile profile) throws IOException {
        return Profile.get(scope, getDefaultTaskURL(profile)).get(NAME);
    }

    public static String getDefaultTaskURL(Profile profile) {
        Vector<String> profiles = profile.loadList(TASKS);
        int index = getDefaultTaskIndex(profile);
        if (index>=0) {
            return profiles.get(index);
        }
        return null;
    }

    public HashMap<String, String> getNames(Profile profile) {
        HashMap<String, String> result = new HashMap<String, String>();
        Vector<String> profiles = profile.loadList(TASKS);
        for (String profileURL : profiles) {
            try {
                String name = Profile.get(scope, profileURL).get(NAME);

                result.put(profileURL, name);
            } catch (IOException ex) {
                Logger.getLogger(scope.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return result;
    }
}
